package
{
	import flash.display.MovieClip;

	public class PlayerObject
	{
		public static var player:PlayerObject;
		public var inputList:Object;
		public var pos:Vector2;
		public var oldpos:Vector2;
		public var r:Number;
		public var xw:Number;
		public var yw:Number;
		public var prevframe:Number;
		public var Tick:Function;
		public var Draw:Function;
		public var mc:MovieClip;
		public var snd:MovieClip;
		public var sndloop:MovieClip;
		// public var sndControl:Sound;
		public var cell;

		public function PlayerObject()
		{
			this.inputList = new Object();
			this.inputList[NinjaGame.PINPUT_L] = false;
			this.inputList[NinjaGame.PINPUT_R] = false;
			this.inputList[NinjaGame.PINPUT_J] = false;
			this.inputList[NinjaGame.PINPUT_JTRIG] = false;
			this.pos = new Vector2(45, 70);
			this.oldpos = this.pos.clone();
			this.r = Main.getInstance().tiles.xw * 0.833333333333333;
			this.xw = this.r;
			this.yw = this.r;
			this.prevframe = 1;
			this.SetupParams();
			Main.getInstance().objects.Register(this);
			this.Tick = this.TickNormal;
			this.Stand();
			this.Draw = this.Draw_Normal;
			player = this;
			this.mc = Main.getInstance().gfx.CreateSprite("testNinjaMCm", NinjaGraphicsSystem.LAYER_PLAYER);
			this.snd = Main.getInstance().gfx.CreateSprite("playerSoundMC", NinjaGraphicsSystem.LAYER_PLAYER);
			this.sndloop = Main.getInstance().gfx.CreateSprite("playerSoundLoopMC", NinjaGraphicsSystem.LAYER_PLAYER);
			// this.sndControl = new Sound(this.sndloop);
		}

		public function Destruct():void
		{
			this.raggy.Destruct();
			this.raggy = null;
			Main.getInstance().gfx.DestroyMC(this.mc);
			this.mc = null;
			Main.getInstance().gfx.DestroyMC(this.snd);
			this.snd = null;
			Main.getInstance().gfx.DestroyMC(this.sndloop);
			this.mc = null;
		}

		public var isDead:Boolean;
		public var timeOfDeath:Number;
		public var maxspeedAir:Number;
		public var maxspeedGround:Number;
		public var groundAccel:Number;
		public var airAccel:Number;
		public var normGrav:Number;
		public var jumpGrav:Number;
		public var normDrag:Number;
		public var winDrag:Number;
		public var wallFriction:Number;
		public var skidFriction:Number;
		public var standFriction:Number;
		public var g:Number;
		public var d:Number;
		public var facingDir:Number;
		public var jumpAmt:Number;
		public var jump_y_bias:Number;
		public var max_jump_time:Number;
		public var terminal_vel:Number;
		public var jumptimer:Number;
		public var WAS_IN_AIR:Boolean;
		public var oldv:Vector2;
		public var IN_AIR:Boolean;
		public var NEAR_WALL:Boolean;
		public var wallN:Vector2;
		public var floorN:Vector2;
		public var floorN0:Vector2;
		public var floorN1:Vector2;
		public var fCount:Number;
		public var CollideVsObjects:Function;

		public function SetupParams():void
		{
			this.isDead = false;
			this.timeOfDeath = 0;
			this.maxspeedAir = this.r * 0.5;
			this.maxspeedGround = this.r * 0.5;
			this.groundAccel = 0.15;
			this.airAccel = 0.1;
			this.normGrav = 0.15;
			this.jumpGrav = 0.025;
			this.normDrag = 0.99;
			this.winDrag = 0.8;
			this.wallFriction = 0.13;
			this.skidFriction = 0.92;
			this.standFriction = 0.8;
			this.g = this.normGrav;
			this.d = this.normDrag;
			this.facingDir = 1;
			this.jumpAmt = 1;
			this.jump_y_bias = 2;
			this.max_jump_time = 30;
			this.terminal_vel = this.r * 0.9;
			this.jumptimer = 0;
			this.WAS_IN_AIR = true;
			this.oldv = new Vector2(0, 0);
			this.IN_AIR = true;
			this.NEAR_WALL = false;
			this.wallN = new Vector2(0, 0);
			this.floorN = new Vector2(0, 0);
			this.floorN0 = new Vector2(0, 0);
			this.floorN1 = new Vector2(0, 0);
			this.fCount = 0;
			this.CollideVsObjects = this.CollideVsObjects2;
		}

		public function Init(params):void
		{
			var _local2 = params;
			if (_local2.length == 2)
			{
				this.pos.x = (this.oldpos.x = _local2[0]);
				this.pos.y = (this.oldpos.y = _local2[1]);
				this.xw = this.r;
				this.yw = this.r;
				this.SetupParams();
				Main.getInstance().objects.AddToGrid(this);
				Main.getInstance().objects.Moved(this);
				Main.getInstance().objects.StartDraw(this);
				this.Tick = this.TickNormal;
				this.Stand();
				this.raggy = new Ragdoll(this.pos, this.r, this.r * 2);
				this.mc.scaleX = (this.mc.scaleY = this.r * 2);
				this.mc.x = this.pos.x;
				this.mc.y = this.pos.y;
			}
		}

		public function UnInit():void
		{
			Main.getInstance().objects.RemoveFromGrid(this);
			Main.getInstance().objects.EndDraw(this);
		}

		public function DumpInitData():String
		{
			return (("" + this.pos.x) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + this.pos.y;
		}

		public function FaceDirection(dir):void
		{
			var _local2 = dir;
			if (this.facingDir == _local2)
			{
			}
			else
			{
				this.facingDir = _local2;
				if (0 < _local2)
				{
					this.mc.scaleX = Math.abs(this.mc.scaleX);
				}
				else
				{
					this.mc.scaleX = -1 * Math.abs(this.mc.scaleX);
				}
			}
		}

		public function TickNormal():void
		{
			var p = this.pos;
			var o = this.oldpos;
			var ox = o.x;
			var oy = o.y;
			var px = ((o.x = p.x));
			var _local3 = (o.y = p.y);
			var _local2 = this.d;
			p.x = p.x + (_local2 * (px - ox));
			p.y = p.y + ((_local2 * (_local3 - oy)) + this.g);
			Main.getInstance().objects.Moved(this);
			this.PrepareToCollide();
			this.CollideVsObjects();
			Main.getInstance().CollideCirclevsTileMap(this);
			this.HandleCollisions();
			Main.getInstance().objects.Moved(this);
			this.Think();
		}

		public var raggy;

		public function TickRagdoll():void
		{
			this.raggy.Tick();
		}

		public function PrepareToCollide():void
		{
			this.oldv.x = this.pos.x - this.oldpos.x;
			this.oldv.y = this.pos.y - this.oldpos.y;
			this.WAS_IN_AIR = this.IN_AIR;
			this.NEAR_WALL = false;
			this.IN_AIR = true;
			this.fCount = 0;
		}

		private function CollideVsObjects2():void
		{
			var _local1;
			var _local3 = this.cell;
			_local1 = _local3.next;
			while (_local1 != null)
			{
				_local1.TestVsPlayer(this);
				_local1 = _local1.next;
			}
			_local1 = _local3.nD.next;
			while (_local1 != null)
			{
				_local1.TestVsPlayer(this);
				_local1 = _local1.next;
			}
			_local1 = _local3.nD.nR.next;
			while (_local1 != null)
			{
				_local1.TestVsPlayer(this);
				_local1 = _local1.next;
			}
			_local1 = _local3.nD.nL.next;
			while (_local1 != null)
			{
				_local1.TestVsPlayer(this);
				_local1 = _local1.next;
			}
			_local1 = _local3.nL.next;
			while (_local1 != null)
			{
				_local1.TestVsPlayer(this);
				_local1 = _local1.next;
			}
			_local1 = _local3.nL.nU.next;
			while (_local1 != null)
			{
				_local1.TestVsPlayer(this);
				_local1 = _local1.next;
			}
			_local1 = _local3.nR.next;
			while (_local1 != null)
			{
				_local1.TestVsPlayer(this);
				_local1 = _local1.next;
			}
			_local1 = _local3.nR.nU.next;
			while (_local1 != null)
			{
				_local1.TestVsPlayer(this);
				_local1 = _local1.next;
			}
			_local1 = _local3.nU.next;
			while (_local1 != null)
			{
				_local1.TestVsPlayer(this);
				_local1 = _local1.next;
			}
		}

		public function HandleCollisions():void
		{
			if (0 < this.fCount)
			{
				this.IN_AIR = false;
				if (1 < this.fCount)
				{
					var fdp = ((this.floorN0.x * this.floorN1.x) + (this.floorN0.y * this.floorN1.y));
					if (0.9 < fdp)
					{
						if ((this.floorN0.x == this.floorN.x) && (this.floorN0.y == this.floorN.y))
						{
						}
						else if ((this.floorN1.x == this.floorN.x) && (this.floorN1.y == this.floorN.y))
						{
						}
						else
						{
							this.floorN.x = this.floorN1.x;
							this.floorN.y = this.floorN1.y;
						}
					}
					else
					{
						var _local2 = this.floorN;
						_local2.x = 0.5 * (this.floorN0.x + this.floorN1.x);
						_local2.y = 0.5 * (this.floorN0.y + this.floorN1.y);
						var len = Math.sqrt((_local2.x * _local2.x) + (_local2.y * _local2.y));
						if (len == 0)
						{
							this.floorN.x = this.floorN0.x;
							this.floorN.y = this.floorN0.y;
						}
						else
						{
							this.floorN.x = _local2.x / len;
							this.floorN.y = _local2.y / len;
						}
					}
				}
				else
				{
					this.floorN.x = this.floorN0.x;
					this.floorN.y = this.floorN0.y;
				}
				if (this.WAS_IN_AIR)
				{
					var hit_dp = ((this.oldv.x * this.floorN.x) + (this.oldv.y * this.floorN.y));
					hit_dp = hit_dp - (2 * Math.abs(this.floorN.y));
					if ((0 < this.oldv.y) && (hit_dp < (-this.terminal_vel)))
					{
						Main.getInstance().game.KillPlayer(NinjaGame.KILLTYPE_FALL, 0, 0, this.pos.x, this.pos.y, this);
					}
				}
			}
			if (this.IN_AIR && (!this.NEAR_WALL))
			{
				var _local3 = this.pos;
				var r = (this.r + 0.1);
				if (TestPoint_Tile.QueryPointvsTileMap(_local3.x + r, _local3.y))
				{
					this.NEAR_WALL = true;
					this.wallN.x = -1;
					this.wallN.y = 0;
				}
				else if (TestPoint_Tile.QueryPointvsTileMap(_local3.x - r, _local3.y))
				{
					this.NEAR_WALL = true;
					this.wallN.x = 1;
					this.wallN.y = 0;
				}
				return;
			}
		}

		public function ReportCollisionVsWorld(px, py, nx, ny, t):void
		{
			var _local2 = ny;
			var _local3 = py;
			this.pos.x = this.pos.x + px;
			this.pos.y = this.pos.y + _local3;
			if ((0.8 * (this.r * this.r)) < ((px * px) + (_local3 * _local3)))
			{
				Main.getInstance().game.KillPlayer(NinjaGame.KILLTYPE_EXPLOSIVE, 0, 0, this.pos.x, this.pos.y, this);
			}
			else if (_local2 == 0)
			{
				this.NEAR_WALL = true;
				this.wallN.x = nx;
				this.wallN.y = _local2;
			}
			else if (_local2 < 0)
			{
				if (this.fCount == 0)
				{
					this.floorN0.x = nx;
					this.floorN0.y = _local2;
					this.fCount++;
				}
				else if ((this.fCount = 1))
				{
					this.floorN1.x = nx;
					this.floorN1.y = _local2;
					this.fCount++;
					return;
				}
			}
		}

		public function ReportCollisionVsObject(px, py, nx, ny, obj):void
		{
			var _local2 = ny;
			var _local3 = nx;
			this.pos.x = this.pos.x + px;
			this.pos.y = this.pos.y + py;
			if (_local2 == 0)
			{
				this.NEAR_WALL = true;
				this.wallN.x = _local3;
				this.wallN.y = _local2;
			}
			else if (_local2 < 0)
			{
				if (this.fCount == 0)
				{
					this.floorN0.x = _local3;
					this.floorN0.y = _local2;
					this.fCount++;
				}
				else if ((this.fCount = 1))
				{
					this.floorN1.x = _local3;
					this.floorN1.y = _local2;
					this.fCount++;
					return;
				}
			}
		}

		public function IdleAfterDeath():void
		{
			this.CollideVsObjects = null;
		}

		//
		public static const PSTATE_STANDING:int = 0;
		public static const PSTATE_RUNNING:int = 1;
		public static const PSTATE_SKIDDING:int = 2;
		public static const PSTATE_JUMPING:int = 3;
		public static const PSTATE_FALLING:int = 4;
		public static const PSTATE_WALLSLIDING:int = 5;
		public static const PSTATE_RAGDOLL:int = 6;
		public static const PSTATE_CELEBRATING:int = 7;
		//
		public var curState;
		public var Think:Function;

		public function Think2():void
		{
			Main.getInstance().game.GetInputState(this.inputList);
			var inList = this.inputList;
			var R = inList[NinjaGame.PINPUT_R];
			var L = inList[NinjaGame.PINPUT_L];
			var J = inList[NinjaGame.PINPUT_J];
			var J_TRIG = inList[NinjaGame.PINPUT_JTRIG];
			var _local2 = this.pos.x - this.oldpos.x;
			var vy = (this.pos.y - this.oldpos.y);
			var curState = this.curState;
			var traction = 1;
			var _local3 = 0;
			if (L)
			{
				_local3 = _local3 - 1;
			}
			if (R)
			{
				_local3 = _local3 + 1;
			}
			if (this.IN_AIR)
			{
				var rot = this.mc.rotation;
				this.mc.rotation = this.mc.rotation - (0.1 * rot);
				var temp = (_local2 + (_local3 * this.airAccel));
				if (Math.abs(temp) < this.maxspeedAir)
				{
					_local2 = temp;
				}
				this.oldpos.x = this.pos.x - _local2;
				if (curState < 3)
				{
					this.Fall();
					return;
				}
				if (curState == PlayerObject.PSTATE_JUMPING)
				{
					this.jumptimer++;
					if ((!J) || (this.jumptimer > this.max_jump_time))
					{
						this.Fall();
						return;
					}
					return;
				}
				else if (curState == PlayerObject.PSTATE_FALLING)
				{
				}
				if (this.NEAR_WALL)
				{
					if (J_TRIG)
					{
						var horiz_amt = 0;
						var vert_amt = 0;
						if ((curState == PlayerObject.PSTATE_WALLSLIDING) && ((_local3 * this.wallN.x) < 0))
						{
							horiz_amt = 1;
							vert_amt = 0.5;
						}
						else
						{
							horiz_amt = 1.5;
							vert_amt = 0.7;
						}
						Main.getInstance().particles.SpawnJumpDust(this.pos.x - (this.wallN.x * this.r), this.pos.y - (this.wallN.y * this.r), this.wallN.x * 90);
						this.Jump(this.wallN.x * horiz_amt, this.wallN.y - vert_amt);
						return;
					}
					if (curState == PlayerObject.PSTATE_WALLSLIDING)
					{
						if (0 < (_local3 * this.wallN.x))
						{
							this.Fall();
							return;
						}
						var ay = Math.abs(vy);
						var fy = ((-(this.wallFriction * traction)) * ay);
						this.oldpos.y = this.pos.y - (vy + fy);
						Main.getInstance().particles.SpawnWallDust(this.pos, this.r, this.wallN, ay);
						var vol = Math.min(100, Math.floor(ay * 70));
						// this.sndControl.setVolume(vol);
						return;
					}
					else if ((0 < vy) && ((_local3 * this.wallN.x) < 0))
					{
						this.Wallslide();
					}
					else
					{
						// unexpected jump
						if (curState == PlayerObject.PSTATE_WALLSLIDING)
						{
							this.Fall();
						}
						else
						{
							return;
							var temp = (_local2 + ((traction * _local3) * this.groundAccel));
							if (Math.abs(temp) < this.maxspeedGround)
							{
								_local2 = temp;
							}
							this.oldpos.x = this.pos.x - _local2;
							if (2 < curState)
							{
								Main.getInstance().particles.SpawnLandDust(this.pos.x - (this.r * this.floorN.x), this.pos.y - (this.r * this.floorN.y), Main.getInstance().NormToRot(this.floorN.x, this.floorN.y) + 90, Math.abs(_local2) + vy);
								this.snd.gotoAndPlay("land");
								if (0 < (_local2 * _local3))
								{
									this.Run(_local3);
									return;
									// unexpected jump
								}
								this.Skid();
							}
							else if (J_TRIG)
							{
								Main.getInstance().particles.SpawnJumpDust(this.pos.x - (this.floorN.x * this.r), this.pos.y - (this.floorN.y * this.r), this.mc.rotation);
								if ((_local3 * this.floorN.x) < 0)
								{
									this.Jump(0, -0.7);
								}
								else
								{
									this.Jump(this.floorN.x, this.floorN.y);
								}
							}
							else if (curState == PlayerObject.PSTATE_RUNNING)
							{
								var nx = this.floorN.x;
								var ny = this.floorN.y;
								var dp = ((_local2 * (-ny)) + (vy * nx));
								var adp = Math.abs(dp);
								var tx = (_local2 * adp);
								if ((_local3 * tx) <= 0)
								{
									this.Skid();
									return;
								}
								if ((_local3 * nx) < 0)
								{
									var fy = (-Math.abs(nx));
									if (nx < 0)
									{
										var fx = (-ny);
									}
									else
									{
										var fx = ny;
									}
									var amt = Math.abs(ny);
									fx = fx * (0.5 * amt);
									fy = fy * (0.5 * amt);
									var tempX = (_local2 + (fx * this.groundAccel));
									var tempY = (vy + (fy * this.groundAccel));
									if (Math.abs(temp) < this.maxspeedGround)
									{
										_local2 = tempX;
										vy = tempY;
									}
									this.oldpos.x = this.pos.x - _local2;
									this.oldpos.y = this.pos.y - vy;
								}
								this.AdvanceRunAnim(_local2, vy, nx, ny);
							}
							else if (curState == PlayerObject.PSTATE_SKIDDING)
							{
								var nx = this.floorN.x;
								var ny = this.floorN.y;
								var dp = Math.abs((_local2 * (-ny)) + (vy * nx));
								var tx = (_local2 * dp);
								if (0 < (tx * _local3))
								{
									this.Run(_local3);
									return;
								}
								Main.getInstance().particles.SpawnFloorDust(this.pos, this.r, this.floorN, this.mc.rotation, this.facingDir, dp);
								if (dp < 0.1)
								{
									this.Stand();
								}
								else
								{
									var f = (this.skidFriction * traction);
									_local2 = _local2 * f;
									this.oldpos.x = this.pos.x - _local2;
									var vol = Math.min(100, Math.floor(dp * 100));
									// this.sndControl.setVolume(vol);
									return;
									return;
									if (_local3 != 0)
									{
										this.Run(_local3);
										return;
									}
									else
									{
										var nx = this.floorN.x;
										var ny = this.floorN.y;
										var dp = Math.abs((_local2 * (-ny)) + (vy * nx));
										if (0.1 <= dp)
										{
											this.Skid();
										}
										else
										{
											var f = (this.standFriction * traction);
											_local2 = _local2 * f;
											vy = vy * f;
											this.oldpos.x = this.pos.x - _local2;
											this.oldpos.y = this.pos.y - vy;
											return;
										}
									}
								}
							}
							else if (_local3 != 0)
							{
								this.Run(_local3);
								return;
							}
							else
							{
								var nx = this.floorN.x;
								var ny = this.floorN.y;
								var dp = Math.abs((_local2 * (-ny)) + (vy * nx));
								if (0.1 <= dp)
								{
									this.Skid();
								}
								else
								{
									var f = (this.standFriction * traction);
									_local2 = _local2 * f;
									vy = vy * f;
									this.oldpos.x = this.pos.x - _local2;
									this.oldpos.y = this.pos.y - vy;
									return;
								}
							}
						}
					}
				}
				else if (curState == PlayerObject.PSTATE_WALLSLIDING)
				{
					this.Fall();
				}
				else
				{
					return;
					var temp = (_local2 + ((traction * _local3) * this.groundAccel));
					if (Math.abs(temp) < this.maxspeedGround)
					{
						_local2 = temp;
					}
					this.oldpos.x = this.pos.x - _local2;
					if (2 < curState)
					{
						Main.getInstance().particles.SpawnLandDust(this.pos.x - (this.r * this.floorN.x), this.pos.y - (this.r * this.floorN.y), Main.getInstance().NormToRot(this.floorN.x, this.floorN.y) + 90, Math.abs(_local2) + vy);
						this.snd.gotoAndPlay("land");
						if (0 < (_local2 * _local3))
						{
							this.Run(_local3);
							return;
							// unexpected jump
						}
						this.Skid();
					}
					else if (J_TRIG)
					{
						Main.getInstance().particles.SpawnJumpDust(this.pos.x - (this.floorN.x * this.r), this.pos.y - (this.floorN.y * this.r), this.mc.rotation);
						if ((_local3 * this.floorN.x) < 0)
						{
							this.Jump(0, -0.7);
						}
						else
						{
							this.Jump(this.floorN.x, this.floorN.y);
						}
					}
					else if (curState == PlayerObject.PSTATE_RUNNING)
					{
						var nx = this.floorN.x;
						var ny = this.floorN.y;
						var dp = ((_local2 * (-ny)) + (vy * nx));
						var adp = Math.abs(dp);
						var tx = (_local2 * adp);
						if ((_local3 * tx) <= 0)
						{
							this.Skid();
							return;
						}
						if ((_local3 * nx) < 0)
						{
							var fy = (-Math.abs(nx));
							if (nx < 0)
							{
								var fx = (-ny);
							}
							else
							{
								var fx = ny;
							}
							var amt = Math.abs(ny);
							fx = fx * (0.5 * amt);
							fy = fy * (0.5 * amt);
							var tempX = (_local2 + (fx * this.groundAccel));
							var tempY = (vy + (fy * this.groundAccel));
							if (Math.abs(temp) < this.maxspeedGround)
							{
								_local2 = tempX;
								vy = tempY;
							}
							this.oldpos.x = this.pos.x - _local2;
							this.oldpos.y = this.pos.y - vy;
						}
						this.AdvanceRunAnim(_local2, vy, nx, ny);
					}
					else if (curState == PlayerObject.PSTATE_SKIDDING)
					{
						var nx = this.floorN.x;
						var ny = this.floorN.y;
						var dp = Math.abs((_local2 * (-ny)) + (vy * nx));
						var tx = (_local2 * dp);
						if (0 < (tx * _local3))
						{
							this.Run(_local3);
							return;
						}
						Main.getInstance().particles.SpawnFloorDust(this.pos, this.r, this.floorN, this.mc.rotation, this.facingDir, dp);
						if (dp < 0.1)
						{
							this.Stand();
						}
						else
						{
							var f = (this.skidFriction * traction);
							_local2 = _local2 * f;
							this.oldpos.x = this.pos.x - _local2;
							var vol = Math.min(100, Math.floor(dp * 100));
							// this.sndControl.setVolume(vol);
							return;
							return;
							if (_local3 != 0)
							{
								this.Run(_local3);
								return;
							}
							else
							{
								var nx = this.floorN.x;
								var ny = this.floorN.y;
								var dp = Math.abs((_local2 * (-ny)) + (vy * nx));
								if (0.1 <= dp)
								{
									this.Skid();
								}
								else
								{
									var f = (this.standFriction * traction);
									_local2 = _local2 * f;
									vy = vy * f;
									this.oldpos.x = this.pos.x - _local2;
									this.oldpos.y = this.pos.y - vy;
									return;
								}
							}
						}
					}
					else if (_local3 != 0)
					{
						this.Run(_local3);
						return;
					}
					else
					{
						var nx = this.floorN.x;
						var ny = this.floorN.y;
						var dp = Math.abs((_local2 * (-ny)) + (vy * nx));
						if (0.1 <= dp)
						{
							this.Skid();
						}
						else
						{
							var f = (this.standFriction * traction);
							_local2 = _local2 * f;
							vy = vy * f;
							this.oldpos.x = this.pos.x - _local2;
							this.oldpos.y = this.pos.y - vy;
							return;
						}
					}
				}
			}
			else
			{
				var temp = (_local2 + ((traction * _local3) * this.groundAccel));
				if (Math.abs(temp) < this.maxspeedGround)
				{
					_local2 = temp;
				}
				this.oldpos.x = this.pos.x - _local2;
				if (2 < curState)
				{
					Main.getInstance().particles.SpawnLandDust(this.pos.x - (this.r * this.floorN.x), this.pos.y - (this.r * this.floorN.y), Main.getInstance().NormToRot(this.floorN.x, this.floorN.y) + 90, Math.abs(_local2) + vy);
					this.snd.gotoAndPlay("land");
					if (0 < (_local2 * _local3))
					{
						this.Run(_local3);
						return;
						// unexpected jump
					}
					this.Skid();
				}
				else if (J_TRIG)
				{
					Main.getInstance().particles.SpawnJumpDust(this.pos.x - (this.floorN.x * this.r), this.pos.y - (this.floorN.y * this.r), this.mc.rotation);
					if ((_local3 * this.floorN.x) < 0)
					{
						this.Jump(0, -0.7);
					}
					else
					{
						this.Jump(this.floorN.x, this.floorN.y);
					}
				}
				else if (curState == PlayerObject.PSTATE_RUNNING)
				{
					var nx = this.floorN.x;
					var ny = this.floorN.y;
					var dp = ((_local2 * (-ny)) + (vy * nx));
					var adp = Math.abs(dp);
					var tx = (_local2 * adp);
					if ((_local3 * tx) <= 0)
					{
						this.Skid();
						return;
					}
					if ((_local3 * nx) < 0)
					{
						var fy = (-Math.abs(nx));
						if (nx < 0)
						{
							var fx = (-ny);
						}
						else
						{
							var fx = ny;
						}
						var amt = Math.abs(ny);
						fx = fx * (0.5 * amt);
						fy = fy * (0.5 * amt);
						var tempX = (_local2 + (fx * this.groundAccel));
						var tempY = (vy + (fy * this.groundAccel));
						if (Math.abs(temp) < this.maxspeedGround)
						{
							_local2 = tempX;
							vy = tempY;
						}
						this.oldpos.x = this.pos.x - _local2;
						this.oldpos.y = this.pos.y - vy;
					}
					this.AdvanceRunAnim(_local2, vy, nx, ny);
				}
				else if (curState == PlayerObject.PSTATE_SKIDDING)
				{
					var nx = this.floorN.x;
					var ny = this.floorN.y;
					var dp = Math.abs((_local2 * (-ny)) + (vy * nx));
					var tx = (_local2 * dp);
					if (0 < (tx * _local3))
					{
						this.Run(_local3);
						return;
					}
					Main.getInstance().particles.SpawnFloorDust(this.pos, this.r, this.floorN, this.mc.rotation, this.facingDir, dp);
					if (dp < 0.1)
					{
						this.Stand();
					}
					else
					{
						var f = (this.skidFriction * traction);
						_local2 = _local2 * f;
						this.oldpos.x = this.pos.x - _local2;
						var vol = Math.min(100, Math.floor(dp * 100));
						// this.sndControl.setVolume(vol);
						return;
						return;
						if (_local3 != 0)
						{
							this.Run(_local3);
							return;
						}
						else
						{
							var nx = this.floorN.x;
							var ny = this.floorN.y;
							var dp = Math.abs((_local2 * (-ny)) + (vy * nx));
							if (0.1 <= dp)
							{
								this.Skid();
							}
							else
							{
								var f = (this.standFriction * traction);
								_local2 = _local2 * f;
								vy = vy * f;
								this.oldpos.x = this.pos.x - _local2;
								this.oldpos.y = this.pos.y - vy;
								return;
							}
						}
					}
				}
				else if (_local3 != 0)
				{
					this.Run(_local3);
					return;
				}
				else
				{
					var nx = this.floorN.x;
					var ny = this.floorN.y;
					var dp = Math.abs((_local2 * (-ny)) + (vy * nx));
					if (0.1 <= dp)
					{
						this.Skid();
					}
					else
					{
						var f = (this.standFriction * traction);
						_local2 = _local2 * f;
						vy = vy * f;
						this.oldpos.x = this.pos.x - _local2;
						this.oldpos.y = this.pos.y - vy;
						return;
					}
				}
			}
		}

		public function ThinkRagdoll():void
		{
		}

		public var Render:Function;
		public var celeb_wasinair:Boolean;

		public function ThinkCelebrate():void
		{
			if (this.IN_AIR)
			{
				if (this.celeb_wasinair)
				{
				}
				else
				{
					this.d = this.normDrag;
					this.Render = this.RenderInAir;
					this.celeb_wasinair = true;
				}
			}
			else
			{
				if (this.celeb_wasinair)
				{
					this.d = this.winDrag;
					this.Render = this.RenderStatic_Ground;
					var _local2 = Main.getInstance().game.GetTime() % 4;
					if (_local2 == 0)
					{
						this.mc.gotoAndPlay("CELEBRATE_NEW1");
					}
					else if (_local2 == 1)
					{
						this.mc.gotoAndPlay("CELEBRATE_NEW2");
					}
					else if (_local2 == 2)
					{
						this.mc.gotoAndPlay("CELEBRATE_NEW3");
					}
					else if (_local2 == 3)
					{
						this.mc.gotoAndPlay("CELEBRATE_NEW4");
					}
				}
				this.celeb_wasinair = false;
			}
		}

		public var ExitState:Function;

		public function Jump(x, y):void
		{
			this.ExitState();
			this.ExitState = this.ExitJump;
			this.curState = PlayerObject.PSTATE_JUMPING;
			this.g = this.jumpGrav;
			var _local3 = this.pos.x - this.oldpos.x;
			var _local2 = this.pos.y - this.oldpos.y;
			if ((_local3 * x) < 0)
			{
				this.oldpos.x = this.pos.x;
			}
			if ((_local2 * y) < 0)
			{
				this.oldpos.y = this.pos.y;
			}
			this.pos.x = this.pos.x + (x * this.jumpAmt);
			this.pos.y = this.pos.y + (y * (this.jumpAmt + this.jump_y_bias));
			this.jumptimer = 0;
			this.mc.rotation = 0;
			this.Render = this.RenderInAir;
			this.snd.gotoAndPlay("jump");
		}

		public function ExitJump():void
		{
			this.g = this.normGrav;
		}

		public function Fall():void
		{
			this.ExitState();
			this.ExitState = this.ExitFall;
			this.curState = PlayerObject.PSTATE_FALLING;
			this.Render = this.RenderInAir;
		}

		public function ExitFall():void
		{
		}

		public function Wallslide():void
		{
			this.ExitState();
			this.ExitState = this.ExitWallslide;
			this.curState = PlayerObject.PSTATE_WALLSLIDING;
			this.FaceDirection(-this.wallN.x);
			this.mc.rotation = 0;
			this.Render = this.RenderWallSlide;
			this.mc.gotoAndStop("WALLSLIDE");
			// this.sndControl.setVolume(0);
			this.sndloop.gotoAndPlay("wallslide_start");
		}

		public function ExitWallslide():void
		{
			this.sndloop.gotoAndPlay("wallslide_stop");
			// this.sndControl.setVolume(100);
		}

		public function Skid():void
		{
			this.ExitState();
			this.ExitState = this.ExitSkid;
			this.curState = PlayerObject.PSTATE_SKIDDING;
			this.Render = this.RenderStatic_Ground;
			this.mc.gotoAndStop("SKID");
			// this.sndControl.setVolume(100);
			this.sndloop.gotoAndPlay("skid_start");
		}

		public function ExitSkid():void
		{
			this.sndloop.gotoAndPlay("skid_stop");
			// this.sndControl.setVolume(100);
		}

		public var runanimleftovers:Number;

		public function Run(dirX):void
		{
			this.ExitState();
			this.ExitState = this.ExitRun;
			this.curState = PlayerObject.PSTATE_RUNNING;
			this.Render = this.RenderRun;
			this.mc.gotoAndStop("RUN");
			this.runanimleftovers = 0;
		}

		public function ExitRun():void
		{
		}

		public function Stand():void
		{
			this.ExitState();
			this.ExitState = this.ExitStand;
			this.curState = PlayerObject.PSTATE_STANDING;
			this.Render = this.RenderStatic_Ground;
			this.mc.gotoAndPlay("STAND");
		}

		public function ExitStand():void
		{
		}

		public function Launch(x, y):void
		{
			this.oldpos.x = this.pos.x;
			this.oldpos.y = this.pos.y;
			this.pos.x = this.pos.x + x;
			this.pos.y = this.pos.y + y;
			this.Fall();
		}

		public function Die(x, y, px, py, KTYPE):void
		{
			var _local2 = y;
			var _local3 = x;
			if (KTYPE == NinjaGame.KILLTYPE_EXPLOSIVE)
			{
				if ((Main.getInstance().game.GetTime() % 2) == 0)
				{
					this.snd.gotoAndPlay("explode1");
				}
				else
				{
					this.snd.gotoAndPlay("explode2");
				}
			}
			else if (KTYPE == NinjaGame.KILLTYPE_FALL)
			{
				this.snd.gotoAndPlay("fall");
			}
			else if (KTYPE == NinjaGame.KILLTYPE_LASER)
			{
				this.snd.gotoAndPlay("laser");
			}
			else if (KTYPE == NinjaGame.KILLTYPE_ELECTRIC)
			{
				if ((Main.getInstance().game.GetTime() % 2) == 0)
				{
					this.snd.gotoAndPlay("zap1");
				}
				else
				{
					this.snd.gotoAndPlay("zap1");
				}
			}
			else if ((Main.getInstance().game.GetTime() % 2) == 0)
			{
				this.snd.gotoAndPlay("shot1");
			}
			else
			{
				this.snd.gotoAndPlay("shot2");
			}
			Main.getInstance().particles.SpawnBloodSpurt(px, py, _local3, _local2, 6 + Math.floor(Math.random() * 8));
			this.ExitState();
			this.ExitState = this.ExitDie;
			this.curState = PlayerObject.PSTATE_RAGDOLL;
			this.Tick = this.TickRagdoll;
			this.Think = null;
			this.Draw = this.Draw_Ragdoll;
			this.mc.visible = false;
			this.isDead = true;
			this.timeOfDeath = Main.getInstance().game.GetTime();
			var vx = (this.pos.x - this.oldpos.x);
			var vy = (this.pos.y - this.oldpos.y);
			this.raggy.Activate();
			this.raggy.MimicMC(vx, vy, this.mc, this.facingDir, this.prevframe);
			if (KTYPE == NinjaGame.KILLTYPE_FALL)
			{
			}
			else
			{
				if (!this.IN_AIR)
				{
					var dp = ((this.floorN.x * _local3) + (this.floorN.y * _local2));
					if (dp < 0)
					{
						var fx = (dp * this.floorN.x);
						var fy = (dp * this.floorN.y);
						var tx = (_local3 - fx);
						var ty = (_local2 - fy);
						Main.getInstance().static_rend.SetStyle(0, 2237064, 100);
						Main.getInstance().static_rend.DrawLine_S(this.pos.x, this.pos.y, this.pos.x + fx, this.pos.y + fy);
						Main.getInstance().static_rend.SetStyle(0, 8921634, 100);
						Main.getInstance().static_rend.DrawLine_S(this.pos.x, this.pos.y, this.pos.x + tx, this.pos.y + ty);
						_local3 = _local3 - (fx * 0.85);
						_local2 = _local2 - (fy * 0.85);
						_local3 = _local3 + (tx * 0.4);
						_local2 = _local2 + (ty * 0.4);
					}
				}
				if (this.NEAR_WALL)
				{
					var dp = ((this.wallN.x * _local3) + (this.wallN.y * _local2));
					if (dp < 0)
					{
						var fx = (dp * this.wallN.x);
						var fy = (dp * this.wallN.y);
						var tx = (_local3 - fx);
						var ty = (_local2 - fy);
						Main.getInstance().static_rend.SetStyle(0, 2237064, 100);
						Main.getInstance().static_rend.DrawLine_S(this.pos.x, this.pos.y, this.pos.x + fx, this.pos.y + fy);
						Main.getInstance().static_rend.SetStyle(0, 8921634, 100);
						Main.getInstance().static_rend.DrawLine_S(this.pos.x, this.pos.y, this.pos.x + tx, this.pos.y + ty);
						_local3 = _local3 - (fx * 0.85);
						_local2 = _local2 - (fy * 0.85);
						_local3 = _local3 + (tx * 0.4);
						_local2 = _local2 + (ty * 0.4);
					}
				}
				this.raggy.Shove_VertBias(_local3, _local2, px, py, this.pos.y, this.r);
			}
			this.TickRagdoll();
		}

		public function ExitDie():void
		{
			if (this.raggy.exploded)
			{
				this.raggy.Unexplode();
			}
			this.raggy.Deactivate();
			this.isDead = false;
			this.timeOfDeath = 0;
			this.Tick = this.TickNormal;
			this.Think = this.Think2;
			this.mc.visible = true;
			this.Draw = this.Draw_Normal;
		}

		public function Celebrate():void
		{
			this.ExitState();
			this.ExitState = this.ExitCelebrate;
			this.curState = PlayerObject.PSTATE_CELEBRATING;
			this.Think = this.ThinkCelebrate;
			this.celeb_wasinair = this.IN_AIR;
		}

		public function ExitCelebrate():void
		{
			this.d = this.normDrag;
			this.Think = this.Think2;
		}

		public function Draw_Normal():void
		{
			this.prevframe = this.mc._currentframe;
			this.Render();
		}

		public function Draw_Ragdoll():void
		{
			this.raggy.Draw();
		}

		public function FaceMovement():void
		{
			var _local1 = this.pos.x - this.oldpos.x;
			if (_local1 == 0)
			{
			}
			else if (0 < _local1)
			{
				this.FaceDirection(1);
			}
			else if (_local1 < 0)
			{
				this.FaceDirection(-1);
				return;
			}
		}

		public function RenderWallSlide():void
		{
			this.mc.x = this.pos.x;
			this.mc.y = this.pos.y;
		}

		public function RenderInAir():void
		{
			this.FaceMovement();
			this.mc.x = this.pos.x;
			this.mc.y = this.pos.y;
			var _local2 = this.pos.y - this.oldpos.y;
			var miny = -1;
			var maxy = 2.5;
			var _local3 = 0;
			if (_local2 < 0)
			{
				if (_local2 < miny)
				{
					_local3 = -1;
				}
				else
				{
					_local3 = -(_local2 / miny);
				}
			}
			else if (maxy < _local2)
			{
				_local3 = 1;
			}
			else
			{
				_local3 = Math.sqrt(_local2 / maxy);
			}
			var offset = Math.floor(_local3 * 9);
			this.mc.gotoAndStop(94 + offset);
		}

		public var runanimcurframe:Number;

		public function RenderRun():void
		{
			this.FaceMovement();
			this.mc.gotoAndStop(this.runanimcurframe);
			this.mc.x = this.pos.x;
			this.mc.y = this.pos.y;
			var _local3 = this.floorN.x;
			var dy = this.floorN.y;
			var _local2 = 0;
			if (_local3 == 0)
			{
				_local2 = -90;
			}
			else if (dy == 0)
			{
				if (_local3 < 0)
				{
					_local2 = 180;
				}
				else
				{
					_local2 = 0;
				}
			}
			else
			{
				_local2 = Math.atan(dy / _local3) / (Math.PI / 180);
				if (_local3 < 0)
				{
					_local2 = _local2 + 180;
				}
			}
			_local2 = _local2 + 90;
			this.mc.rotation = _local2;
		}

		public function AdvanceRunAnim(vx, vy, nx, ny):void
		{
			var len = Math.abs((vx * (-ny)) + (vy * nx));
			var _local3 = 13;
			var framevel = 0.9;
			var runanimlen = 72;
			var curframe = (this.mc._currentframe - _local3);
			var _local1 = len / framevel;
			_local1 = _local1 + this.runanimleftovers;
			var framestepfloor = Math.floor(_local1);
			this.runanimleftovers = _local1 - framestepfloor;
			var nextframe = ((curframe + framestepfloor) % runanimlen);
			this.runanimcurframe = _local3 + nextframe;
		}

		public function RenderDebug():void
		{
			Main.getInstance().static_rend.SetStyle(0, 0, 25);
			Main.getInstance().static_rend.DrawAABB(this.pos, this.xw, this.yw);
			Main.getInstance().static_rend.DrawCircle(this.pos, this.r);
		}

		public function RenderStatic():void
		{
			this.FaceMovement();
			this.mc.x = this.pos.x;
			this.mc.y = this.pos.y;
		}

		public function RenderStatic_Ground():void
		{
			this.FaceMovement();
			this.mc.x = this.pos.x;
			this.mc.y = this.pos.y;
			var _local3 = this.floorN.x;
			var dy = this.floorN.y;
			var _local1 = 0;
			if (_local3 == 0)
			{
				_local1 = -90;
			}
			else if (dy == 0)
			{
				if (_local3 < 0)
				{
					_local1 = 180;
				}
				else
				{
					_local1 = 0;
				}
			}
			else
			{
				_local1 = Math.atan(dy / _local3) / (Math.PI / 180);
				if (_local3 < 0)
				{
					_local1 = _local1 + 180;
				}
			}
			_local1 = _local1 + 90;
			this.mc.rotation = _local1;
		}
	}
}